import { HTTP } from '../utils/http-p.js'

class KeywordModel extends HTTP{
  key = 'q'
  maxLength = 10

  getHistory() {
    const words = wx.getStorageSync(this.key)
    if(!words){
      return []
    }
    return words
  }

  // getHot() {
  //   return this.request({
  //     url: '/book/hot_keyword'
  //   })
  // }

  addToHistory(keyword) {
    if (keyword == null) {
      return
    }
    let words = this.getHistory()
    const has = words.includes(keyword)
    if (!has) {
      const length = words.length;
      if(length>=this.maxLength){
        words.pop()
      }
      words.unshift(keyword)
      wx.setStorageSync(this.key, words)
    }
  }

  addHotWord(hotWord){
    return this.request({
      url: '/v/hot/addHotWord',
      data:{
        hotWord:hotWord
      }
    })
  }
}

export { KeywordModel}
